#!/usr/bin/env python
"""
   Copyright 2006-2009 SpringSource (http://springsource.com), All Rights Reserved

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.       
"""
__version__ = "${version}"

import os
import re
import sys
import tarfile
import time
import getopt
import shutil
import urllib
from datetime import datetime

############################################################################
# Get external properties and load into a dictionary. NOTE: These properties
# files mimic Java props files.
############################################################################

p = {}
p["plugindir"] = os.path.expanduser("~") + "/.springpython"
sys.path.append(p["plugindir"])

if not os.path.exists(p["plugindir"]):
    os.makedirs(p["plugindir"])

############################################################################
# Statically defined functions.
############################################################################

def plugin_path(name):
    return p["plugindir"] + "/%s" % name

def list_installed_plugins():
    print "Looking for plugins in %s..." % p["plugindir"]
    if not os.path.exists(p["plugindir"]):
        os.makedirs(p["plugindir"])
    print "Currently installed plugins:"
    for plugin in os.listdir(p["plugindir"]):
        try:
            if hasattr(p[plugin], "create"):
                print "\t--" + plugin + " [name]"+ "\t"*(3-len(plugin)/8) + p[plugin].__description__
            else:
                print "\t--" + plugin + "\t"*(4-len(plugin)/8) + p[plugin].__description__
        except KeyError:
            print "\t%s is not valid plugin" % plugin
        except AttributeError:
            print "\t%s is not a valid plugin" % plugin

def list_available_plugins():
    modules = get_modules_from_s3()
    print "S3 from SpringSource has..."
    for key in modules:
        for rev in modules[key]:
            if __version__ in rev[1]:
                print "\t%s\t%s" % (key, rev[1])

def install_plugin(name):
    """Go through a series of options to look for Spring Python modules to download."""
    if not os.path.exists(plugin_path(name)):
        print "Installing plugin %s to %s" % (name, p["plugindir"])
        if fetch_from_s3(name):
            print "Found %s online. Installing." % name
            return
        if fetch_locally(name):
            print "Found %s locally. Installing." % name
            return
        print "Couldn't find %s anywhere!" % name
    else:
        print "%s is already installed. We do NOT support automated upgrades." % name

def get_modules_from_s3():
    """Read the S3 site for information about existing modules."""
    info = ""
    for bucket in ["milestone", "release"]:
        url = "http://s3browse.springsource.com/browse/dist.springframework.org/%s/EXTPY" % bucket
        info += urllib.urlopen(url).read()
    #print info
    #return []
    #choicesR = re.compile('<a href="/getObject/(.*?)">(.*?)</a>.*?<td align="right">(.*?)</td>.*?<td align="right">(.*?)</td>', re.DOTALL)
    choicesR = re.compile(r'<td class="name-column"><a href="(.*?)">(.*?)</a>', re.DOTALL)
    match = choicesR.findall(info)
    converted = []
    for item in match:
        #print "Checking out %s" % str(item)
        if "springpython-plugin" not in item[0]: continue
        if item[0].endswith(".sha1"): continue 
        #print "Parsing %s" % item[0]
        converted.append(item)
        #t = time.strptime(item[3], "%Y-%m-%d %H:%M")
        #d = datetime(year=t.tm_year, month=t.tm_mon, day=t.tm_mday, hour=t.tm_hour, minute=t.tm_min)
        #converted.append(("http://s3.amazonaws.com/" + item[0], item[1], item[2]+"K", item[3], d))
    modules = {}
    baseR = re.compile("springpython-plugin-([a-zA-Z-]*)[-.]([0-9a-zA-Z]+.*).tar.gz")
    for item in converted:
        try:
            modules[baseR.match(item[1]).group(1)].append(item)
        except KeyError:
            modules[baseR.match(item[1]).group(1)] = [item]
    return modules
  
def fetch_from_s3(name):
    """Download a selected item"""
    print "Can we find %s online? " % name
    selected = None
    try:
        s3 = get_modules_from_s3()
        print "Looking at %s" % s3
        print "Ah ha! I can see %s" % str(s3[name])
        for item in s3[name]:
            print "Is %s the version we want?" % str(item[1])
            if __version__ in item[1]:
                selected = item
    except KeyError, e:
        print "KeyError! %s" % e
        pass

    if selected is None:
        print "Couldn't find %s online!" % name
        return None
    else:
        print "Fetching %s as %s" % (selected[0], selected[1])
        urllib.urlretrieve(selected[0], selected[1])
        t = tarfile.open(selected[1], "r:gz")
        top = t.getmembers()[0]
        if os.path.exists(name):
            shutil.rmtree(name)
        for member in t.getmembers():
            t.extract(member)
        fetch_locally(name)
        shutil.rmtree(name)
        os.remove(selected[1])
        return True

def fetch_locally(name):
    """Scan the local directory for Spring Python modules to install."""
    try:
        shutil.copytree(name, plugin_path(name))
        return True
    except OSError:
        print "Couldn't find %s locally!" % name
        return None

def uninstall_plugin(name):
    if os.path.exists(plugin_path(name)):
        print "Uninstalling plugin %s from %s" % (name, p["plugindir"])
        shutil.rmtree(plugin_path(name))
    else:
        print "Plugin %s is NOT installed." % name

############################################################################
# Print out a listing of existing commands, including hooks to installed
# plugins.
############################################################################

def usage():
    """This tool helps you create Spring Python apps, install plug-ins, and much more."""
    print
    print "Usage: coily [command]"
    print
    print "\t--help\t\t\t\tprint this help message"
    print "\t--list-installed-plugins\tlist currently installed plugins"
    print "\t--list-available-plugins\tlist plugins available for download"
    print "\t--install-plugin [name]\t\tinstall coily plugin"
    print "\t--uninstall-plugin [name]\tuninstall coily plugin"
    print "\t--reinstall-plugin [name]\treinstall coily plugin"
    for plugin in p["plugins"]:
        try:
            if hasattr(p[plugin], "create"):
                print "\t--" + plugin + " [name]"+ "\t"*(3-len(plugin)/8) + p[plugin].__description__
            else:
                print "\t--" + plugin + "\t"*(4-len(plugin)/8) + p[plugin].__description__
        except AttributeError, e:
            print "\t%s/%s is not a valid plugin => %s" % (p["plugindir"], plugin, e)
    print

print "Coily v%s - the command-line management tool for Spring Python, http://springpython.webfactional.com" % __version__
print "==============================================================================="
print "Copyright 2006-2009 SpringSource (http://springsource.com), All Rights Reserved"
print "Licensed under the Apache License, Version 2.0"
print

############################################################################
# Read the command-line, and assemble commands. Any invalid command, print
# usage info, and EXIT.
############################################################################

p["plugins"] = []
import_errors = False
for plugin in os.listdir(p["plugindir"]):
    try:
        p[plugin] = __import__(plugin)
        p["plugins"].append(plugin)
    except ImportError:
        #print "\tWARNING: %s is not a python package, making it an invalid plugin." % plugin
        #import_errors = True
        pass
if import_errors:
    print

try:
    cmds = ["help",
            "list-installed-plugins",
            "list-available-plugins",
            "install-plugin=", 
            "uninstall-plugin=",
            "reinstall-plugin="]
    cmds.extend([plugin + "=" for plugin in p["plugins"] if hasattr(p[plugin], "create")])
    cmds.extend([plugin       for plugin in p["plugins"] if not hasattr(p[plugin], "create")])
    optlist, args = getopt.getopt(sys.argv[1:], "h", cmds)
except getopt.GetoptError:
    # print help information and exit:
    print "Invalid command found in %s" % sys.argv
    usage()
    sys.exit(2)

# Check for help requests, which cause all other options to be ignored. Help can offer version info, which is
# why it comes as the second check
for option in optlist:
    if option[0] in ("--help", "-h"):
        usage()
        sys.exit(1)
        
############################################################################
# Main commands. Skim the options, and run each command as its found.
# Commands are run in the order found ON THE COMMAND LINE.
############################################################################

# Parse the arguments, in order
for option in optlist:
    if option[0] in ("--list-installed-plugins"):
        list_installed_plugins()

    if option[0] in ("--list-available-plugins"):
        list_available_plugins()

    if option[0] in ("--install-plugin"):
        install_plugin(option[1])

    if option[0] in ("--uninstall-plugin"):
        uninstall_plugin(option[1])

    if option[0] in ("--reinstall-plugin"):
        uninstall_plugin(option[1])
        install_plugin(option[1])

    cmd = option[0][2:]  # Command name with "--" stripped out
    if cmd in p["plugins"]:
        try:
            p[cmd].create(p["plugindir"] + "/" + cmd, option[1])
        except AttributeError, e:
            print e
            p[cmd].apply(p["plugindir"] + "/" + cmd)

